package com.tensquare.base.controller;

import com.tensquare.base.Service.LabelService;
import com.tensquare.base.pojo.Label;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/* @RequestMapping 来映射请求，也就是通过它来指定控制器可以处理哪些URL请求,类似于struts的action请求
 * @responsebody表示该方法的返回结果直接写入HTTP response body中
 *一般在异步获取数据时使用，在使用@RequestMapping后，返回值通常解析为跳转路径，加上@responsebody后返回结果不会被解析为跳转路径，而是直接写入HTTP response *body中。
 *比如异步获取json数据，加上@responsebody后，会直接返回json数据。
 *
 *@Pathvariable注解绑定它传过来的值到方法的参数上
 *用于将请求URL中的模板变量映射到功能处理方法的参数上，即取出uri模板中的变量作为参数
 *@CrossOriginSpring Framework 4.2 GA为CORS提供了第一类支持，使您比通常的基于过滤器的解决方案更容易和更强大地配置它。所以springMVC的版本要在4.2或以上版本才支持@CrossOrigin
 */
@RestController
@CrossOrigin//解决跨域问题
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private LabelService labelService;

    /**
     * 查询所有标签
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        List<Label> list = labelService.findAll();
        if (list == null || list.size() == 0) {
            return new Result(false, StatusCode.RESULT_NULL, "没有你要查询的结果！");
        }
        return new Result(true, StatusCode.OK, "查询成功！", list);
    }

    @RequestMapping(value = "/{labelId}", method = RequestMethod.GET)
    public Result findById(@PathVariable("labelId") String labelId) {
        Label label = labelService.findById(labelId);
        if (label == null) {
            return new Result(false, StatusCode.RESULT_NULL, "没有你要查询的结果！");
        }
        return new Result(true, StatusCode.OK, "查询成功！", label);
    }

    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Label label) {
        labelService.add(label);
        return new Result(true, StatusCode.OK, "添加成功！");
    }

    @RequestMapping(value = "/{labelId}", method = RequestMethod.PUT)
    public Result update(@PathVariable("labelId") String labelId, @RequestBody Label label) {
        label.setId(labelId);
        labelService.update(label);
        return new Result(true, StatusCode.OK, "修改成功！");
    }

    @RequestMapping(value = "/{labelId}", method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable("labelId") String labelId) {
        labelService.deleteById(labelId);
        return new Result(true, StatusCode.OK, "删除成功！");
    }

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Label label) {
        List<Label> list = labelService.findSearch(label);
        if (list == null || list.size() == 0) {
            return new Result(false, StatusCode.RESULT_NULL, "没有你要查询的结果！");
        }
        return new Result(true, StatusCode.OK, "查询成功！", list);
    }

    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result pageQuery(@RequestBody Label label, @PathVariable int page, @PathVariable int size) {
        Page<Label> pageList = labelService.pageQuery(label, page, size);
        if (pageList == null || pageList.getTotalElements() == 0) {
            return new Result(false, StatusCode.RESULT_NULL, "没有你要查询的结果！");
        }
        return new Result(true, StatusCode.OK, "查询成功!", new PageResult<Label>
                (pageList.getTotalElements(), pageList.getContent()));
    }

    //热门标签
    @RequestMapping(value = "/toplist", method = RequestMethod.GET)
    public Result topList() {
        List<Label> list = labelService.topList();
        if (list == null || list.size() == 0) {
            return new Result(false, StatusCode.RESULT_NULL, "没有你要查询的结果！");
        }
        return new Result(true, StatusCode.OK, "查询成功！", list);
    }

    //根据问题id查询标签
    @RequestMapping(value = "/listbypid", method = RequestMethod.POST)
    public Result findByids(@RequestBody String[] ids) {
        for (int i=0;i<ids.length;i++){
            System.err.println("参数："+ids[i]);
        }
        List<Label> list = labelService.findByids(ids);
        if (list == null || list.size() == 0) {
            return new Result(false, StatusCode.RESULT_NULL, "没有你要查询的结果！");
        }
        return new Result(true, StatusCode.OK, "查询成功！", list);
    }
}
